package com.wolf.system.controller;

import com.wolf.vo.PageResult;
import com.wolf.provider.exception.ERROR_MSG;
import com.wolf.provider.exception.WolfException;
import com.wolf.provider.order.info.PageBean;
import com.wolf.provider.util.JsonUtil;
import com.wolf.system.model.SysUser;
import com.wolf.system.service.ISysUserService;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

/**
 * 系统用户Controller
 */
@Controller
@RequestMapping(value = "/sysUser")
public class SysUserController {

    private static final String SUCCESS_MSG = "success";
    private static final String FAIL_MSG = "fail";

    @Autowired
    private ISysUserService manager;

    /**
     *  跳转用户列表页面
     * @return
     */
    @RequestMapping(value = "/sysUser")
    @ResponseBody
    public ModelAndView sysUserView(){
        ModelAndView model = new ModelAndView();
        model.setViewName("system/sys_user_view");
        model.addObject("currentUrl","/sysUser/sysUser");
        SysUser sysUser = manager.getSessionSysUser();
        model.addObject("sysUser",sysUser);
        model.addObject("currentTitle","系统用户");
        return model;
    }

    /**
     * 显示页面数据信息
     * @param draw
     * @param start
     * @param length
     * @param queryData
     * @return
     */
    @RequestMapping(value = "/sysUserInfoData")
    @ResponseBody
    public PageResult sysUserInfoData(@RequestParam(value = "draw") int draw,
                               @RequestParam(value = "start") int start,
                               @RequestParam(value = "length") int length,
                               @RequestParam(value = "queryData") String queryData){

        PageBean pageBean = new PageBean();
        pageBean.setStart(start);
        pageBean.setPageSize(length);

        SysUser model = new SysUser();

        if(StringUtils.isNotEmpty(queryData)){
            JSONObject jsonParams = JSONObject.fromObject(queryData);
            if(jsonParams.has("userName")){
                model.setUserName(jsonParams.get("userName").toString());
            }
            if(jsonParams.has("status")){
                model.setStatus(jsonParams.get("status").toString());
            }
        }

        PageResult result = manager.findByPage(model, pageBean);
        result.setDraw(draw);

        return result;
    }


    /**
     * 保存数据
     * @param requestData
     * @return
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public String saveData(@RequestParam(value = "requestData") String requestData){
        if(StringUtils.isEmpty(requestData)){
            WolfException.report(ERROR_MSG.ERROR_STRING_001);
        }
        SysUser sysUser = manager.getSessionSysUser();
        SysUser sysUserModel = JsonUtil.parseObject(requestData,SysUser.class);
        if(sysUserModel != null && sysUserModel.getId() != null){
            sysUserModel.setModifyPerson(sysUser.getUserName());
            sysUserModel.setModifyTime(new Date());
            boolean isResult = manager.updateSysUser(sysUserModel);
            if(isResult){
                return SUCCESS_MSG;
            }
            return FAIL_MSG;
        }
        sysUserModel.setCreator(sysUser.getUserName());
        sysUserModel.setCreateTime(new Date());
        boolean isResult = manager.addSysUser(sysUserModel);

        if(isResult){
            return SUCCESS_MSG;
        }
        return FAIL_MSG;
    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public String deleteData(@RequestParam(value = "id") Long id){
        SysUser model = new SysUser();
        model.setId(id);
        boolean isResult = manager.deleteSysUser(model);
        if(isResult){
            return SUCCESS_MSG;
        }
        return FAIL_MSG;
    }


}
